package com.msh.bird;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

/**
 * Date：2021年09月
 * Author：程Sir
 * Content:程序运行的主类
 */
public class Main extends JPanel {
    /**
     * 飞扬的小鸟
     */
    public Bird bird;
    /**
     * 游戏中的柱子
     */
    public Column column1, column2, column3;
    /**
     * 游戏中的地面
     */
    public Ground ground;
    /**
     * 游戏中的背景
     */
    public BufferedImage background;
    /**
     * 游戏开始状态
     */
    public boolean started;
    /**
     * 游戏开始界面
     */
    public BufferedImage startImage;
    /**
     * 游戏结束状态
     */
    public boolean gameOver;
    /**
     * 游戏结束界面
     */
    public BufferedImage gameOverImage;
    /**
     * 游戏用户头像
     */
    public BufferedImage myImage;
    /**
     * 游戏所得分数
     */
    public int score;

    /**
     * 游戏中碰撞的声音
     */
    public Music hitMusic;
    /**
     * 游戏中飞行的声音
     */
    public Music flappyMusic;
    /**
     * 游戏中得分的声音
     */
    public Music scoreMusic;
    /**
     * 游戏中开始的声音
     */
    public Music startMusic;

    /**
     * 构造方法：实现所以属性的初始化动作
     * @throws Exception 初始化过程中发生的异常
     */
    public Main() throws Exception {
        startMusic = new Music("music/start.wav");// 所有的名称和路径是不可以写错的，否则或出现运行异常
        hitMusic = new Music("music/hit.wav");
        flappyMusic = new Music("music/flappy.wav");
        scoreMusic = new Music("music/score.wav");

        started =false;
        startImage = ImageIO.read(getClass().getResource("img/start.png"));
        gameOver = false;
        gameOverImage = ImageIO.read(getClass().getResource("img/gameover.png"));
        myImage = ImageIO.read(getClass().getResource("img/my.png"));
        bird = new Bird();
        score = 0;
        column1 = new Column(1);
        column2 = new Column(2);
        column3 = new Column(3);
        ground = new Ground();
        background = ImageIO.read(getClass().getResource("img/background.png"));

    }

    /**
     * 绘制所有对象
     * @param g 画笔
     */
    @Override
    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, null);
        g.drawImage(column1.image, column1.x - column1.width / 2, column1.y - column1.height / 2, null);
        g.drawImage(column2.image, column2.x - column2.width / 2, column2.y - column2.height / 2, null);
        g.drawImage(column3.image, column3.x - column3.width / 2, column3.y - column3.height / 2, null);
        g.drawImage(ground.image, ground.x, ground.y, null);
        g.drawImage(myImage, 20, 530, 100, 100, null);
        //旋转绘图坐标系
        Graphics2D g2 = (Graphics2D) g;
        g2.rotate(-bird.angle, bird.x, bird.y);
        g.drawImage(bird.image, bird.x - bird.width / 2, bird.y - bird.height / 2, null);
        g2.rotate(bird.angle, bird.x, bird.y);
        //判断游戏运行的状态
        if (gameOver) {
            g.drawImage(gameOverImage, 134, 0, null);
        }
        if (!started) {
            g.drawImage(startImage, 0, 0, null);
        }
        //绘制分数等字符串组件
        Font f = new Font(Font.SANS_SERIF, Font.BOLD, 24);
        g.setFont(f);
        g.drawString("得分：", 140, 570);
        g.setColor(Color.WHITE);
        g.drawString("得分：", 140 - 3, 570 - 3);

        f = new Font(Font.SANS_SERIF, Font.BOLD, 40);
        g.setFont(f);
        g.setColor(Color.BLACK);
        g.drawString("" + score, 160, 610);
        g.setColor(Color.WHITE);
        g.drawString("" + score, 160 - 3, 610 - 3);
        f = new Font(Font.SANS_SERIF, Font.BOLD, 25);
        g.setFont(f);
        g.drawString("和女朋友一起玩才有意思！" , 280, 570 );
        f = new Font(Font.SANS_SERIF, Font.BOLD, 18);
        g.setFont(f);
        g.setColor(Color.red);
        g.drawString("欢迎关注程sir抖音：编程Sir" , 280, 600 );
        g.drawString("微信公众号：程Sir小课堂。获取更多学习资料" +
                "" , 280, 630 );
    }

    /**
     * 程序执行的总方法
     * @throws InterruptedException  程序中断异常
     */
    public void action() throws InterruptedException {
        // 鼠标监听事件
        MouseListener l = new MouseAdapter() {
            //鼠标按下触发功能，e表示触发的事件
            public void mousePressed(MouseEvent e) {
                try {
                    if (gameOver) {
                        synchronized (Main.this) {
                            column1 = new Column(1);
                            column2 = new Column(2);
                            column3 = new Column(3);
                            bird = new Bird();
                            started = false;
                            gameOver = false;
                            score = 0;
                            startMusic.play();
                        }
                    } else {
                        started = true;
                        bird.flappy();
                        flappyMusic.play();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        this.addMouseListener(l);
        while (true) {
            if (!gameOver) {
                if (started) {
                    column1.step();
                    column2.step();
                    column3.step();
                    bird.step();
                }
                bird.fly();
                ground.step();
                // 计分逻辑
                if (bird.x == column1.x ||bird.x == column2.x || bird.x == column3.x) {
                    score++;
                    scoreMusic.play();
                }
            }
            // 同步锁：实现线程安全！
            synchronized (Main.this) {
                // 如果鸟撞上地面游戏就结束了
                if (bird.hit(ground) ||bird.hit(column1) ||bird.hit(column2) || bird.hit(column3)) {
                    gameOver = true;
                    hitMusic.play();
                }
            }
            // 调用父类的画画方法，重复往复的画界面上的所有组件
            repaint();
            // 小鸟点击之后的延时
            Thread.sleep(1000 / 60);
        }
    }

    /**
     * 启动程序的主方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args)throws Exception {
        // 界面 = = = 画框
        JFrame frame = new JFrame("程Sir小课堂 - 游戏：飞扬的小鸟");
        // 创建JPanel面板对象
        Main game = new Main();
        frame.add(game);
        // 设置窗口的尺寸
        frame.setSize(700, 690);
        // 设置框口的现实位置：居中
        frame.setLocationRelativeTo(null);
        // 窗口默认的关闭方式：关闭窗口，游戏停止
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口的可见性：true表示显示，false表示不显示
        frame.setVisible(true);
        // 窗口总是置顶显示
        frame.setAlwaysOnTop(true);
        // 调用运行的主程序方法
        game.action();
    }
}
